package com.shengzai.opt


import org.apache.spark.sql.{DataFrame, SparkSession}
import org.apache.spark.storage.StorageLevel

object Demo2CarCache {
  def main(args: Array[String]): Unit = {
    val spark: SparkSession = SparkSession
      .builder()
      .master("local[8]")
      .appName("car")
      .getOrCreate()



    val carDF: DataFrame = spark.read
      .format("csv")
      .option("sep", ",")
      .schema("tollgate_id STRING, license_plate STRING, timestamp STRING, longitude STRING, latitude STRING, speed STRING, city STRING, road STRING, district STRING")
      .load("data/cars.log")

    /**
     * 不做缓存的情况
     */
    import org.apache.spark.sql.functions._
    import spark.implicits._
    carDF.persist(StorageLevel.MEMORY_ONLY)

    val districtCount: DataFrame = carDF.groupBy($"district")
      .agg(count($"district"))
    val cityCount: DataFrame = carDF.groupBy($"city")
      .agg(count($"city"))
    val roadDF: DataFrame = carDF.groupBy($"road")
      .agg(count($"road"))


    districtCount.show()
    cityCount.show()
    roadDF.show()

    while (true){

    }



  }

}
